package com.ydj.app.service;

import com.ydj.app.entity.query.CmsAccountQuery;
import com.ydj.app.protocol.http.oldcms.*;
import com.ydj.framework.exception.CommonException;
import com.ydj.app.entity.CmsAccount;
import com.ydj.app.entity.dao.CmsAccountDao;
import com.ydj.framework.mysql.PageArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * CMS账户相关服务
 */
@Slf4j
@Service
public class CmsAccountService {

    @Autowired
    CmsAccountDao cmsAccountDao;

    /**
     * 获取账号列表
     */
    public CmsAccountGetAccountListRes cmsGetAccountList(CmsAccountGetAccountListReq req, CmsAccount account) {
        PageArgs pageArgs = req.getPageArgs();
        List<CmsAccount> list = new CmsAccountQuery(cmsAccountDao)
                .username(req.getUsername())
                .nameLike(req.getName())
                .page(pageArgs);

        CmsAccountGetAccountListRes res = new CmsAccountGetAccountListRes(pageArgs);
        for (CmsAccount cmsAccount : list) {
            CmsAccountGetAccountListRes.CmsAccountGetAccountListResItem item = new CmsAccountGetAccountListRes.CmsAccountGetAccountListResItem()
                    .setUsername(cmsAccount.getUsername())
                    .setName(cmsAccount.getName())
                    .setCreateTime(cmsAccount.getCreateTime())
                    .setEnable(cmsAccount.getEnable());
            res.getList().add(item);
        }
        return res;
    }

    /**
     * 创建账号
     */
    public CmsAccountCreateAccountRes cmsCreateAccount(CmsAccountCreateAccountReq req, CmsAccount account) throws CommonException {
        long count = new CmsAccountQuery(cmsAccountDao)
                .username(req.getUsername())
                .count();
        CommonException.throwExceptionIfTrue(count > 0, String.format("账号【%s】已存在", req.getUsername()));

        CmsAccount cmsAccount = new CmsAccount()
                .setUsername(req.getUsername())
                .setName(req.getName())
                .setPassword("admin123")
                .setEnable(true);
        cmsAccountDao.saveOrUpdate(cmsAccount);

        return new CmsAccountCreateAccountRes()
                .setId(cmsAccount.getId());
    }

    /**
     * 修改账号
     */
    public void cmsUpdateAccount(CmsAccountUpdateAccountReq req, CmsAccount account) throws CommonException {
        CmsAccount existAccount = new CmsAccountQuery(cmsAccountDao)
                .username(req.getUsername())
                .single();
        if (existAccount != null && !existAccount.getId().equals(req.getId())) {
            CommonException.throwException(String.format("账号【%s】已存在", req.getUsername()));
        }

        CmsAccount cmsAccount = new CmsAccountQuery(cmsAccountDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(cmsAccount, "找不到该账号信息");

        cmsAccount.setUsername(req.getUsername())
                .setName(req.getName());
        cmsAccountDao.saveOrUpdate(cmsAccount);
    }

    /**
     * 更新密码
     */
    public void cmsUpdatePassword(CmsAccountUpdatePasswordReq req, CmsAccount account) throws CommonException {
        CmsAccount cmsAccount = new CmsAccountQuery(cmsAccountDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(cmsAccount, "找不到该账号信息");

        // 修改
        cmsAccount.setPassword(req.getPassword());
        cmsAccountDao.saveOrUpdate(cmsAccount);
    }

    /**
     * 修改是否启用
     */
    public void cmsUpdateEnable(CmsAccountUpdateEnableReq req, CmsAccount account) throws CommonException {
        CmsAccount cmsAccount = new CmsAccountQuery(cmsAccountDao)
                .id(req.getId())
                .single();
        CommonException.throwExceptionIfNull(cmsAccount, "找不到该账号信息");

        // 修改
        cmsAccount.setEnable(req.getEnable());
        cmsAccountDao.saveOrUpdate(cmsAccount);
    }
}